package com.example.zsfinacialmanage.activity;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import com.example.zsfinacialmanage.R;
import com.example.zsfinacialmanage.adapter.IncomeAdapter;
import com.example.zsfinacialmanage.bean.IncomeBean;
import com.example.zsfinacialmanage.utils.MyDBHelper;
import java.util.ArrayList;
import java.util.List;

public class InComeDetailActivity extends AppCompatActivity {
    RecyclerView recy_view;
    MyDBHelper myDBHelper;
    SQLiteDatabase db;
    List<IncomeBean> arr1 = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_in_come_detail);
        initView();
        initData();
        IncomeAdapter adapter = new IncomeAdapter(InComeDetailActivity.this,arr1);
        StaggeredGridLayoutManager st = new StaggeredGridLayoutManager(StaggeredGridLayoutManager.VERTICAL,1);
        recy_view.setLayoutManager(st);
        recy_view.setAdapter(adapter);
    }

    private void initView() {
        recy_view = findViewById(R.id.recy_view_indetail);
        myDBHelper = new MyDBHelper(InComeDetailActivity.this);
        db = myDBHelper.getWritableDatabase();
    }

    private void initData() {
        //从数据库查询所有已存数据，并取出
        Cursor cursor = db.rawQuery("select * from in_come",null);
        if (cursor != null && cursor.moveToFirst()){
            do{
                @SuppressLint("Range") int myid = cursor.getInt(cursor.getColumnIndex("id"));
                @SuppressLint("Range") double mymoney = cursor.getDouble(cursor.getColumnIndex("inmoney"));
                @SuppressLint("Range") String mytime = cursor.getString(cursor.getColumnIndex("intime"));
                @SuppressLint("Range") String mytype = cursor.getString(cursor.getColumnIndex("intype"));
                @SuppressLint("Range") String mypayer = cursor.getString(cursor.getColumnIndex("inpayer"));
                @SuppressLint("Range") String myremark = cursor.getString(cursor.getColumnIndex("inremark"));
                IncomeBean incomeBean = new IncomeBean(myid,mymoney,mytime,mytype,mypayer,myremark);
                arr1.add(incomeBean);
            }while (cursor.moveToNext());
        }
        cursor.close();
    }
}